package com.suzhe.ymall.cart.controller;


import com.suzhe.ymall.cart.pojo.CartSku;
import com.suzhe.ymall.cart.service.ICartService;
import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/cart/")
@Slf4j
public class CartController {
    @Autowired
    ICartService iCartService;

    @LoginAuth
    @GetMapping("/listByUserId")
    public ApiResEntity list(Integer userId){
        List<CartSku> cartSkus = iCartService.findByUserId(userId);
        return ApiResEntity.ok(cartSkus);
    }

    @LoginAuth
    @PostMapping("/save")
    public ApiResEntity save(CartSku cartSku){
        iCartService.saveOrUpdate(cartSku);
        return ApiResEntity.ok();
    }

    @LoginAuth
    @PostMapping("/updateNum")
    public ApiResEntity updateNum(CartSku cartSku){
        iCartService.updateNum(cartSku);
        return ApiResEntity.ok();
    }
}
